package org.supermy.batch.esb;


import java.io.File;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.integration.launch.JobLaunchRequest;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.annotation.MessageEndpoint;
import org.springframework.integration.annotation.ServiceActivator;

/**
 * @author jamesmo
 *
 */
@MessageEndpoint
public class FileToJobLaunchRequestAdapter implements InitializingBean {

    private Job job;

    public void setJob(Job job) {
        this.job = job;
    }

    public void afterPropertiesSet() throws Exception {
       // Assert.notNull(job, "A Job must be provided");
    }

    @ServiceActivator
    public JobLaunchRequest adapt(File file) throws NoSuchJobException {

        String fileName = file.getAbsolutePath();

        if (!fileName.startsWith("/")) {
            fileName = "/" + fileName;
        }

        fileName = "file://" + fileName;
        
        String outPutFilePath = "file:I:/env-myopensource/mybigdata/src/test/resources/integration/out/out.xml";

        JobParameters jobParameters = new JobParametersBuilder().
                addString("input.file.path", fileName).
                addString("output.file.path", outPutFilePath).
                addLong("time.stamp", System.currentTimeMillis()).
                toJobParameters();

        if (job.getJobParametersIncrementer() != null) {
            jobParameters = job.getJobParametersIncrementer().getNext(jobParameters);
        }

        return new JobLaunchRequest(job, jobParameters);

    }

}